var currentUser;               //points to the document of the user who is logged in

function populateUserInfo() {
    // find out who is logged in 
    var who = JSON.parse(localStorage.getItem("loggedinUser"));  //person who is logged in
    var users = JSON.parse(localStorage.getItem("users"));   //the whole users database
    let email = who["email"];                                //the person who is logged in, email
    console.log(email);
    const user = users.find(u => u.email === email);         //find the person with that email
    console.log(user);
    currentUser = user;

    // get information for that user who is logged in
    if (user) {
        console.log("Currently logged in:  " + user["name"]);
        let userName = user["name"];
        let userCity = user["city"];
        let userSchool = user["school"];

        //if the data fields are not empty, then write them in to the form.
        if (userName != null) {
            document.getElementById("nameInput").value = userName;
        }
        if (userSchool != null) {
            document.getElementById("schoolInput").value = userSchool;
        }
        if (userCity != null) {
            document.getElementById("cityInput").value = userCity;
        }

    } else {
        // No user is signed in.
        console.log("No user is signed in");
    }
}

//call the function to run it 
populateUserInfo();


function editUserInfo() {
    //Enable the form fields
    document.getElementById('personalInfoFields').disabled = false;
 }



 function saveUserInfo() {
    //enter code here

    //a) get user entered values
userName = document.getElementById('nameInput').value;       //get the value of the field with id="nameInput"
userSchool = document.getElementById('schoolInput').value;     //get the value of the field with id="schoolInput"
userCity = document.getElementById('cityInput').value;       //get the value of the field with id="cityInput"
    //b) update user's document in DB
    updateProfileDB(userName, userSchool, userCity);
function updateProfileDB(name, school, city) {
    var who = JSON.parse(localStorage.getItem("loggedinUser"));
    if (who) {

        var users = JSON.parse(localStorage.getItem("users"));
        var email = who["email"];  //email of who is logged in
        console.log(email);        //debug

        if (users) {
            //find the person who is logged in, from the users collection
            const person = users.find(u => u.email === email);
            if (person) {

	              // Bonus Challenge!
                  person.school = school;
                  person.name = name;
                  person.city = city;

                  localStorage.setItem("users", JSON.stringify(users));
                
                alert('Profile data saved successfully!');
             document.getElementById('personalInfoFields').disabled = true;    
            }
        }
    } else {
        console.log("No one is logged in");
    }
}
    //c) disable edit 
   
}







